﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 01.04.2008
 * Zeit: 13:39
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;
using Gamadus.Engine;

namespace Gamadus.Builder.Ast
{
	/// <summary>
	/// Description of Function.
	/// </summary>
	public class Function : Expression
	{
		readonly string m_name;
		readonly Expression[] m_arguments;
			
		public Function(Irony.Compiler.AstNode node, SourceLocation location) : base(node, location)
		{
			m_name = (node.ChildNodes[0] as Irony.Compiler.Token).Text.ToLower();
			if(!EngineMgr.Instance.Functions.Contains(m_name))
				Console.WriteLine("Unknown function "+m_name);
			if(node.ChildNodes.Count == 1)
			{
				m_arguments = new Expression[0]; // no arguments
			}
			else if(node.ChildNodes[1].Term.Name == "Expr_list")
			{
				m_arguments = new Expression[node.ChildNodes[1].ChildNodes.Count];
				int i = 0;
				foreach(Irony.Compiler.AstNode child in node.ChildNodes[1].ChildNodes)
				{
					m_arguments[i++] = Expression.MakeExpression(child, new SourceLocation(location, child.Location));
				}
			}
			else
			{
				m_arguments = new Expression[] {Expression.MakeExpression(node.ChildNodes[1],new SourceLocation(location, node.ChildNodes[1].Location))};
			}
		}
		
		public override string Generate()
		{
			string arg_list = "";
			foreach(Expression expr in m_arguments)
			{
				arg_list += expr.Generate()+", ";
			}
			return "Gml."+m_name+"("+arg_list.Substring(0,arg_list.Length-2)+")";
		}
		
//		protected internal override Microsoft.Scripting.Ast.Expression Generate()
//		{
//			System.Reflection.MethodInfo method = typeof(GML).GetMethod(m_name);
//			if(method == null)
//				throw new ApplicationException("Function "+m_name+" does not exist");
//			MSAst.Expression[] expressions = new MSAst.Expression[m_arguments.Length];
//			int i = 0;
//			foreach(Expression expr in m_arguments)
//			{
//				expressions[i] = MSAst.Ast.ConvertHelper(m_arguments[i].Generate(), typeof(GmlVar));
//				i++;
//			}
//			return MSAst.Ast.Statement(
//                Span,
//                MSAst.Ast.Call(method, expressions)
//            );
//		}
		
		public override string ToString()
		{
			string ret = "Function call: "+m_name+"(";
			foreach(Expression expr in m_arguments)
			{
				ret += expr.ToString()+", ";
			}
			ret = (m_arguments.Length > 0 ? ret.Substring(0, ret.Length-2) : ret) +")";
			return ret;
		}
	}
}
